package com.internship.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.internship.model.Admin;
import com.internship.model.DepartmentInfo;
import com.internship.model.Notify;
import com.internship.model.Paper;
import com.internship.model.StudentInfo;
import com.internship.model.TeacherInfo;
import com.internship.page.Page;
import com.internship.service.AdminService;
import com.internship.service.Const;
import com.internship.service.NotifyService;
import com.internship.service.FileUpload;
import com.internship.service.PaperService;
import com.internship.service.StudentInfoService;
import com.internship.service.TeacherInfoService;
import com.internship.util.StringUtil;


@Controller
@RequestMapping("notify")
public class NotifyController {
	private final Logger logger = Logger.getLogger(NotifyController.class);

	@Autowired
	private AdminService adminService;
	@Autowired
	private NotifyService notifyService;
	@Autowired
	private StudentInfoService 	studentInfoService;
	@Autowired
	private TeacherInfoService teacherInfoService;
	public static HttpSession getSession() {
		HttpSession session = null;
		try {
			session = getRequest().getSession();
		} catch (Exception e) {
		}
		return session;
	}

	public static HttpServletRequest getRequest() {
		ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes();
		return attrs.getRequest();
	}
	private void clearSession() {
		getSession().invalidate();
	}
	
	@RequestMapping("/notify_list.do")
	public ModelAndView getNotifyList() {
		return getNotifyList(null,1);
	}
	
	@RequestMapping("/notify_page_list.do")
	public ModelAndView getNotifyList(@RequestParam String title,@RequestParam int pageNumber) {
		ModelAndView mv = new ModelAndView("notify");
		HttpSession session = getSession();
		try {
			if (String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("2")||String.valueOf(session.getAttribute(Const.USER_ROLE)).equals("1")) {
				Page<Notify> pageList = notifyService.getNotifyList(title,0, pageNumber);
				mv.addObject("currentPage", pageNumber);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("notifyList", pageList.getResultList());
			}else {
				Page<Notify> pageList = notifyService.getNotifyList(title,Integer.parseInt((String) session.getAttribute(Const.SESSION_ACCOUNT)), pageNumber);
				mv.addObject("currentPage", pageNumber);
				mv.addObject("totalPage", pageList.getTotalPage());
				mv.addObject("totalCount", pageList.getTotalCount());
				mv.addObject("notifyList", pageList.getResultList());
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		mv.addObject("title", title);
		return mv;
	}
	/**?
	 * 打招呼页面
	 * @return
	 */
	@RequestMapping("/greet.do")
	public ModelAndView greet(@RequestParam("schoolTeacher")String schoolTeacher) {
		HttpSession session = getSession();
		ModelAndView mv = new ModelAndView("greet");
		TeacherInfo teacherInfo = teacherInfoService.geTeacherInfoByZGH(schoolTeacher);
		StudentInfo  studentInfo = studentInfoService.getStudentInfoByXH((String) session.getAttribute(Const.SESSION_ACCOUNT));
		mv.addObject("teacherInfo", teacherInfo);
		mv.addObject("studentInfo", studentInfo);
		return mv;
	}
	@RequestMapping("/add_notify.do")
	@ResponseBody
	public JSONObject addNotify(@RequestParam("schoolTeacher")String  schoolTeacher,@RequestParam("title")String  title,@RequestParam("content")String  content) {
		JSONObject json = new JSONObject();
		HttpSession session = getSession();
		Notify notify =new Notify();
			Admin admin = adminService.getAdminByteacherNumber(schoolTeacher);
			if(admin!=null){
				notify.setToId(admin.getTeacherNumber());
				notify.setToUserName(admin.getTeacherName());
				notify.setToUserRole(admin.getUserRole());
			}else {
				TeacherInfo teacherInfo = teacherInfoService.geTeacherInfoByZGH(schoolTeacher);
				if (teacherInfo!=null) {
					Admin admin1 = adminService.getAdminByteacherNumber(teacherInfo.getZGH());
					if(admin1==null){
						notify.setToUserRole(4);
					}else {
						notify.setToUserRole(admin1.getUserRole());
					}
					notify.setToId(teacherInfo.getZGH());
					notify.setToUserName(teacherInfo.getXM());
				}else {
					StudentInfo  studentInfo = studentInfoService.getStudentInfoByXH(schoolTeacher);
					notify.setToId(studentInfo.getXH());
					notify.setToUserName(studentInfo.getXM());
					notify.setToUserRole(6);
				}
		}
		notify.setTitle(title);
		notify.setContent(content);
		notify.setFromId(String.valueOf(session.getAttribute(Const.SESSION_ACCOUNT)));
		notify.setFromUserName(String.valueOf(session.getAttribute(Const.USER_NAME)));
		notify.setFromUserRole(Integer.parseInt(String.valueOf(session.getAttribute(Const.USER_ROLE))));
		notify.setToUserRead(0);
		notify.setPubTime(StringUtil.getStrDateTime());
		int rel =notifyService.addNotify(notify);
		json.put("result", rel>0?"success":"error");
		return json;
	}
	@RequestMapping("/notify_chcek.do")
	public ModelAndView notifyChcek(@RequestParam("id")int id) {
		ModelAndView mv = new ModelAndView("notify_check");
		Notify notify= notifyService.getNotifyById(id);
		notify.setToUserRead(1);
		notifyService.updateNotify(notify);
		mv.addObject("notify", notify);
		return mv;
	}
	@RequestMapping("/notify_reply.do")
	public ModelAndView notifyRely(@RequestParam("id")int id) {
		ModelAndView mv = new ModelAndView("notify_reply");
		Notify notify= notifyService.getNotifyById(id);
		notify.setToUserRead(1);
		notifyService.updateNotify(notify);
		mv.addObject("notify", notify);
		return mv;
	}
	/**?
	 * 删除
	 * @param Id
	 * @return
	 */
		@RequestMapping("/delete.do")
		@ResponseBody
		public JSONObject delPaper(@RequestParam int id){
			JSONObject json = new JSONObject();
			int  rel= notifyService.delNotify(id);
			json.put("result", rel > 0 ?"success":"error");
			 return json; 
		}
			
		}
